package com.jxwifi.cloud.quickclean.assemble;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.idroid.utils.NoDoubleClickListener;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.app.AccountMgr;
import com.jxwifi.cloud.quickclean.app.ClientBasicActivity;
import com.jxwifi.cloud.quickclean.app.StateUtils;
import com.jxwifi.cloud.quickclean.bean.AssemRecordsBean;
import com.jxwifi.cloud.quickclean.main.WebViewsActivity;
import com.jxwifi.cloud.quickclean.pay.PayResult;
import com.jxwifi.cloud.quickclean.utils.KAlertDialog;
import com.jxwifi.cloud.quickclean.utils.TitleClient;
import com.jxwifi.cloud.quickclean.utils.ToastUtils;
import com.jxwifi.cloud.quickclean.utils.shareUtils;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.simple.eventbus.Subscriber;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 拼单列表进入的详情
 */
public class ADetailsActivity extends ClientBasicActivity implements ADetailsView {

    @Bind(R.id.img_assemble_details)
    ImageView mImgAssembleDetails;
    @Bind(R.id.tv_assemble_details_name)
    TextView mTvAssembleDetailsName;
    @Bind(R.id.tv_assemble_details_price)
    TextView mTvAssembleDetailsPrice;
    @Bind(R.id.tv_assemble_details_original_price)
    TextView mTvAssembleDetailsOriginalPrice;
    @Bind(R.id.tv_assemble_details_description)
    TextView mTvAssembleDetailsDescription;
    @Bind(R.id.tv_assemble_time)
    TextView mTvAssembleTime;
    @Bind(R.id.tv_assemble_all_number)
    TextView mTvAssembleAllNumber;
    @Bind(R.id.tv_assemble_ortrue)
    TextView mTvAssembleOrTrue;
    @Bind(R.id.tv_assemble_numbering)
    TextView mTvAssembleNumbering;
    @Bind(R.id.tv_assemble_difference)
    TextView mTvAssembleDifference;
    @Bind(R.id.tv_assemble_details_shar_it)
    TextView mTvAssembleDetailsSharIt;
    @Bind(R.id.tv_assemble_details_over)
    TextView mTvAssembleDetaislOver;
    @Bind(R.id.tv_assemble_details_cancel)
    TextView mTvAssembleDetailsCancel;
    @Bind(R.id.tv_assemble_people)
    TextView mTvAssemblePeople;
    @Bind(R.id.tv_assemble_details_give_away)
    TextView mTvAssembleDetailsGiveAway;
    @Bind(R.id.tv_assemble_details_specification)
    TextView mTvAssembleDetailsSpecification;
    @Bind(R.id.wv_details_show)
    WebView mWvDetailsShow;
    @Bind(R.id.tv_assemble_details_button)
    TextView mTvAssembleDetailsButton;
    @Bind(R.id.tv_assemble_starts_time)
    TextView mTvAssembleStartsTime;

    private AssemRecordsBean mDetailsBean;

    private ADetailsPresenter mPresenter;

    private String mOrderType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_assemble_adetails);
        ButterKnife.bind(this);
        openEventBus();
        mDetailsBean = (AssemRecordsBean) getIntent().getExtras().getSerializable("assemble_details");
        mOrderType = getIntent().getStringExtra("order_type");
        //Log.i(TAG,"onCreate ==> mOrderType = "+mOrderType);
        initHead();
        mPresenter = new ADetailsPresenter(mBaseContext, this);
        mPresenter.initDatailsData();
    }

    private void initHead() {
        TitleClient holder = new TitleClient(ButterKnife.findById(this, R.id.lin_client_title));
        if (StateUtils.SR.equals(mDetailsBean.getCtype())) {
            holder.defineTitle("套餐的拼单");
        } else if (StateUtils.SN.equals(mDetailsBean.getCtype())) {
            holder.defineTitle("工具包的拼单");
        } else if (StateUtils.TD.equals(mDetailsBean.getCtype())) {
            holder.defineTitle("清洁服务的拼单");
        }
        holder.defineLeft(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                onBackPressed();
            }
        });
    }

    @OnClick({R.id.tv_assemble_details_shar_it,R.id.tv_assemble_details_cancel,R.id.tv_assemble_details_over,R.id.tv_assemble_details_description
    ,R.id.tv_assemble_details_button})
    void clickCK(View v) {
        Intent intent=new Intent();
        switch (v.getId()) {
            //分享
            case R.id.tv_assemble_details_shar_it:
                WeXinFen();
                break;
                //取消
            case R.id.tv_assemble_details_cancel:
                final KAlertDialog dialogshow=new KAlertDialog(mBaseContext,"确认是否取消拼团");
                dialogshow.setRightTxt("确定", new NoDoubleClickListener() {
                    @Override
                    protected void onNoDoubleClick(View v) {

                        mPresenter.assembleCancel();
                        dialogshow.dismiss();
                    }
                });
                dialogshow.show();
                break;
                //已结束
            case R.id.tv_assemble_details_over:

                break;
            case R.id.tv_assemble_details_description:
                intent.setClass(mBaseContext, WebViewsActivity.class);
                intent.putExtra("web_view_url", "https://ui.qcleaner.cn/resource/assemble.html");
                intent.putExtra("web_view_title", "拼团说明");
                startActivity(intent);
                break;
            case R.id.tv_assemble_details_button:
                if(mPresenter.getPeopleName().contains(AccountMgr.getName(mBaseContext))){
                    ToastUtils.showMyToast(Toast.makeText(mBaseContext, "你已经加入该拼团了哦，在“我的拼单”可查询！.", Toast.LENGTH_SHORT), 1000);
                    return;
                }
                mPresenter.paySetMeal();
                break;
        }
    }

    public void WeXinFen() {
        IWXAPI mIWXAPI = WXAPIFactory.createWXAPI(mBaseContext, StateUtils.WX_APPID, false);
        mIWXAPI.registerApp(StateUtils.WX_APPID);
        mPresenter.weChatShare(mIWXAPI, 1, mBaseContext);
    }

    @Subscriber(tag = "e-PayResult")
    private void getCondItion(PayResult mBean) {
        Toast toast = Toast.makeText(mBaseContext, "支付成功", Toast.LENGTH_SHORT);
        ToastUtils.showMyToast(toast, 3000);
        mPresenter.RefreshView();
    }



    @Subscriber(tag = "e-WXPayResult")
    private void getWXCondItion(PayResult mBean) {
        Toast toast = Toast.makeText(mBaseContext, "支付成功", Toast.LENGTH_SHORT);
        ToastUtils.showMyToast(toast, 3000);
        mPresenter.RefreshView();
    }

    @Override
    public TextView getStartsTime() {
        return mTvAssembleStartsTime;
    }

    @Override
    public TextView getDetailsButton() {
        return mTvAssembleDetailsButton;
    }

    @Override
    public String getOrderType() {
        return mOrderType;
    }

    @Override
    public void setDetailsBean(AssemRecordsBean bean) {
        mDetailsBean=bean;
    }

    @Override
    public void setOrderType(String type) {
        mOrderType=type;
    }

    @Override
    public TextView getSpecification() {
        return mTvAssembleDetailsSpecification;
    }

    @Override
    public TextView getDetailsGiveAway() {
        return mTvAssembleDetailsGiveAway;
    }

    @Override
    public WebView getWebViewShow() {
        return mWvDetailsShow;
    }

    @Override
    public TextView getAssemblePeople() {
        return mTvAssemblePeople;
    }

    @Override
    public TextView getDetaislOver() {
        return mTvAssembleDetaislOver;
    }

    @Override
    public TextView getDetailsCancel() {
        return mTvAssembleDetailsCancel;
    }

    @Override
    public TextView getDetailsSharIt() {
        return mTvAssembleDetailsSharIt;
    }

    @Override
    public TextView getDifference() {
        return mTvAssembleDifference;
    }

    @Override
    public TextView getNumbering() {
        return mTvAssembleNumbering;
    }

    @Override
    public TextView getOrTrue() {
        return mTvAssembleOrTrue;
    }

    @Override
    public TextView getAllNumber() {
        return mTvAssembleAllNumber;
    }

    @Override
    public TextView getAssembleTime() {
        return mTvAssembleTime;
    }

    @Override
    public TextView getDescription() {
        return mTvAssembleDetailsDescription;
    }

    @Override
    public TextView getOriginalPrice() {
        return mTvAssembleDetailsOriginalPrice;
    }

    @Override
    public TextView getDetailsPrice() {
        return mTvAssembleDetailsPrice;
    }

    @Override
    public TextView getDetailsName() {
        return mTvAssembleDetailsName;
    }

    @Override
    public AssemRecordsBean getDetailsBean() {
        return mDetailsBean;
    }

    @Override
    public void onFailure(int errorCode, String msg) {
        Toast toast = Toast.makeText(mBaseContext, msg, Toast.LENGTH_SHORT);
        ToastUtils.showMyToast(toast, 3000);
    }

    @Override
    public void onCancelSuccess() {
        Toast toast = Toast.makeText(mBaseContext, "取消成功", Toast.LENGTH_SHORT);
        ToastUtils.showMyToast(toast, 3000);
        finish();
    }

    @Override
    public ImageView getDetailsImages() {
        return mImgAssembleDetails;
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
}
